{
  "cells": [
    {
      "cell_type": "markdown",
      "source": [
        "# Logistic Regression Classification Part 3"
      ],
      "metadata": {}
    },
    {
      "cell_type": "code",
      "source": [
        "import numpy as np\n",
        "import matplotlib.pyplot as plt\n",
        "import pandas as pd\n",
        "\n",
        "import warnings\n",
        "warnings.filterwarnings(\"ignore\")\n",
        "\n",
        "# yahoo finance is used to fetch data \n",
        "import yfinance as yf\n",
        "yf.pdr_override()"
      ],
      "outputs": [],
      "execution_count": 1,
      "metadata": {
        "collapsed": false,
        "outputHidden": false,
        "inputHidden": false,
        "execution": {
          "iopub.status.busy": "2022-06-26T01:23:14.185Z",
          "iopub.execute_input": "2022-06-26T01:23:14.190Z",
          "shell.execute_reply": "2022-06-26T01:23:14.437Z",
          "iopub.status.idle": "2022-06-26T01:23:14.418Z"
        }
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# input\n",
        "symbol = 'AMD'\n",
        "start = '2014-01-01'\n",
        "end = '2018-08-27'\n",
        "\n",
        "# Read data \n",
        "dataset = yf.download(symbol,start,end)\n",
        "\n",
        "# View Columns\n",
        "dataset.head()"
      ],
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "[*********************100%***********************]  1 of 1 completed\n"
          ]
        },
        {
          "output_type": "execute_result",
          "execution_count": 2,
          "data": {
            "text/plain": "            Open  High   Low  Close  Adj Close    Volume\nDate                                                    \n2014-01-02  3.85  3.98  3.84   3.95       3.95  20548400\n2014-01-03  3.98  4.00  3.88   4.00       4.00  22887200\n2014-01-06  4.01  4.18  3.99   4.13       4.13  42398300\n2014-01-07  4.19  4.25  4.11   4.18       4.18  42932100\n2014-01-08  4.23  4.26  4.14   4.18       4.18  30678700",
            "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>Open</th>\n      <th>High</th>\n      <th>Low</th>\n      <th>Close</th>\n      <th>Adj Close</th>\n      <th>Volume</th>\n    </tr>\n    <tr>\n      <th>Date</th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>2014-01-02</th>\n      <td>3.85</td>\n      <td>3.98</td>\n      <td>3.84</td>\n      <td>3.95</td>\n      <td>3.95</td>\n      <td>20548400</td>\n    </tr>\n    <tr>\n      <th>2014-01-03</th>\n      <td>3.98</td>\n      <td>4.00</td>\n      <td>3.88</td>\n      <td>4.00</td>\n      <td>4.00</td>\n      <td>22887200</td>\n    </tr>\n    <tr>\n      <th>2014-01-06</th>\n      <td>4.01</td>\n      <td>4.18</td>\n      <td>3.99</td>\n      <td>4.13</td>\n      <td>4.13</td>\n      <td>42398300</td>\n    </tr>\n    <tr>\n      <th>2014-01-07</th>\n      <td>4.19</td>\n      <td>4.25</td>\n      <td>4.11</td>\n      <td>4.18</td>\n      <td>4.18</td>\n      <td>42932100</td>\n    </tr>\n    <tr>\n      <th>2014-01-08</th>\n      <td>4.23</td>\n      <td>4.26</td>\n      <td>4.14</td>\n      <td>4.18</td>\n      <td>4.18</td>\n      <td>30678700</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
          },
          "metadata": {}
        }
      ],
      "execution_count": 2,
      "metadata": {
        "collapsed": false,
        "outputHidden": false,
        "inputHidden": false,
        "execution": {
          "iopub.status.busy": "2022-06-26T01:23:14.424Z",
          "iopub.execute_input": "2022-06-26T01:23:14.429Z",
          "iopub.status.idle": "2022-06-26T01:23:15.098Z",
          "shell.execute_reply": "2022-06-26T01:23:15.131Z"
        }
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# Create more data\n",
        "dataset['Increase/Decrease'] = np.where(dataset['Volume'].shift(-1) > dataset['Volume'],1,0)\n",
        "dataset['Buy_Sell_on_Open'] = np.where(dataset['Open'].shift(-1) > dataset['Open'],1,-1)\n",
        "dataset['Buy_Sell'] = np.where(dataset['Adj Close'].shift(-1) > dataset['Adj Close'],1,-1)\n",
        "dataset['Return'] = dataset['Adj Close'].pct_change()\n",
        "dataset = dataset.dropna()\n",
        "dataset['Up_Down'] = np.where(dataset['Return'].shift(-1) > dataset['Return'],'Up','Down')\n",
        "dataset.head()"
      ],
      "outputs": [
        {
          "output_type": "execute_result",
          "execution_count": 3,
          "data": {
            "text/plain": "            Open  High   Low  Close  Adj Close    Volume  Increase/Decrease  \\\nDate                                                                          \n2014-01-03  3.98  4.00  3.88   4.00       4.00  22887200                  1   \n2014-01-06  4.01  4.18  3.99   4.13       4.13  42398300                  1   \n2014-01-07  4.19  4.25  4.11   4.18       4.18  42932100                  0   \n2014-01-08  4.23  4.26  4.14   4.18       4.18  30678700                  0   \n2014-01-09  4.20  4.23  4.05   4.09       4.09  30667600                  0   \n\n            Buy_Sell_on_Open  Buy_Sell    Return Up_Down  \nDate                                                      \n2014-01-03                 1         1  0.012658      Up  \n2014-01-06                 1         1  0.032500    Down  \n2014-01-07                 1        -1  0.012106    Down  \n2014-01-08                -1        -1  0.000000    Down  \n2014-01-09                -1         1 -0.021531      Up  ",
            "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>Open</th>\n      <th>High</th>\n      <th>Low</th>\n      <th>Close</th>\n      <th>Adj Close</th>\n      <th>Volume</th>\n      <th>Increase/Decrease</th>\n      <th>Buy_Sell_on_Open</th>\n      <th>Buy_Sell</th>\n      <th>Return</th>\n      <th>Up_Down</th>\n    </tr>\n    <tr>\n      <th>Date</th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>2014-01-03</th>\n      <td>3.98</td>\n      <td>4.00</td>\n      <td>3.88</td>\n      <td>4.00</td>\n      <td>4.00</td>\n      <td>22887200</td>\n      <td>1</td>\n      <td>1</td>\n      <td>1</td>\n      <td>0.012658</td>\n      <td>Up</td>\n    </tr>\n    <tr>\n      <th>2014-01-06</th>\n      <td>4.01</td>\n      <td>4.18</td>\n      <td>3.99</td>\n      <td>4.13</td>\n      <td>4.13</td>\n      <td>42398300</td>\n      <td>1</td>\n      <td>1</td>\n      <td>1</td>\n      <td>0.032500</td>\n      <td>Down</td>\n    </tr>\n    <tr>\n      <th>2014-01-07</th>\n      <td>4.19</td>\n      <td>4.25</td>\n      <td>4.11</td>\n      <td>4.18</td>\n      <td>4.18</td>\n      <td>42932100</td>\n      <td>0</td>\n      <td>1</td>\n      <td>-1</td>\n      <td>0.012106</td>\n      <td>Down</td>\n    </tr>\n    <tr>\n      <th>2014-01-08</th>\n      <td>4.23</td>\n      <td>4.26</td>\n      <td>4.14</td>\n      <td>4.18</td>\n      <td>4.18</td>\n      <td>30678700</td>\n      <td>0</td>\n      <td>-1</td>\n      <td>-1</td>\n      <td>0.000000</td>\n      <td>Down</td>\n    </tr>\n    <tr>\n      <th>2014-01-09</th>\n      <td>4.20</td>\n      <td>4.23</td>\n      <td>4.05</td>\n      <td>4.09</td>\n      <td>4.09</td>\n      <td>30667600</td>\n      <td>0</td>\n      <td>-1</td>\n      <td>1</td>\n      <td>-0.021531</td>\n      <td>Up</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
          },
          "metadata": {}
        }
      ],
      "execution_count": 3,
      "metadata": {
        "collapsed": false,
        "outputHidden": false,
        "inputHidden": false,
        "execution": {
          "iopub.status.busy": "2022-06-26T01:23:15.107Z",
          "iopub.execute_input": "2022-06-26T01:23:15.112Z",
          "shell.execute_reply": "2022-06-26T01:23:15.136Z",
          "iopub.status.idle": "2022-06-26T01:23:15.147Z"
        }
      }
    },
    {
      "cell_type": "code",
      "source": [
        "dataset.shape"
      ],
      "outputs": [
        {
          "output_type": "execute_result",
          "execution_count": 4,
          "data": {
            "text/plain": "(1170, 11)"
          },
          "metadata": {}
        }
      ],
      "execution_count": 4,
      "metadata": {
        "collapsed": false,
        "outputHidden": false,
        "inputHidden": false,
        "execution": {
          "iopub.status.busy": "2022-06-26T01:23:15.155Z",
          "iopub.execute_input": "2022-06-26T01:23:15.160Z",
          "iopub.status.idle": "2022-06-26T01:23:15.168Z",
          "shell.execute_reply": "2022-06-26T01:23:15.139Z"
        }
      }
    },
    {
      "cell_type": "code",
      "source": [
        "X = dataset.drop(['Up_Down', 'Close'], axis=1)  \n",
        "y = dataset['Up_Down'] "
      ],
      "outputs": [],
      "execution_count": 5,
      "metadata": {
        "collapsed": true,
        "jupyter": {
          "source_hidden": false,
          "outputs_hidden": false
        },
        "nteract": {
          "transient": {
            "deleting": false
          }
        },
        "execution": {
          "iopub.status.busy": "2022-06-26T01:23:15.176Z",
          "iopub.execute_input": "2022-06-26T01:23:15.181Z",
          "iopub.status.idle": "2022-06-26T01:23:15.189Z",
          "shell.execute_reply": "2022-06-26T01:23:15.211Z"
        }
      }
    },
    {
      "cell_type": "code",
      "source": [
        "from sklearn.preprocessing import PolynomialFeatures"
      ],
      "outputs": [],
      "execution_count": 6,
      "metadata": {
        "collapsed": false,
        "outputHidden": false,
        "inputHidden": false,
        "execution": {
          "iopub.status.busy": "2022-06-26T01:23:15.195Z",
          "iopub.execute_input": "2022-06-26T01:23:15.201Z",
          "iopub.status.idle": "2022-06-26T01:23:15.973Z",
          "shell.execute_reply": "2022-06-26T01:23:15.966Z"
        }
      }
    },
    {
      "cell_type": "code",
      "source": [
        "pol = PolynomialFeatures(2)"
      ],
      "outputs": [],
      "execution_count": 7,
      "metadata": {
        "collapsed": true,
        "jupyter": {
          "source_hidden": false,
          "outputs_hidden": false
        },
        "nteract": {
          "transient": {
            "deleting": false
          }
        },
        "execution": {
          "iopub.status.busy": "2022-06-26T01:23:15.980Z",
          "iopub.execute_input": "2022-06-26T01:23:15.986Z",
          "iopub.status.idle": "2022-06-26T01:23:15.994Z",
          "shell.execute_reply": "2022-06-26T01:23:16.036Z"
        }
      }
    },
    {
      "cell_type": "code",
      "source": [
        "X = pol.fit_transform(X)"
      ],
      "outputs": [],
      "execution_count": 8,
      "metadata": {
        "collapsed": false,
        "outputHidden": false,
        "inputHidden": false,
        "execution": {
          "iopub.status.busy": "2022-06-26T01:23:16.001Z",
          "iopub.execute_input": "2022-06-26T01:23:16.007Z",
          "iopub.status.idle": "2022-06-26T01:23:16.015Z",
          "shell.execute_reply": "2022-06-26T01:23:16.041Z"
        }
      }
    },
    {
      "cell_type": "code",
      "source": [
        "from sklearn.linear_model import LogisticRegression"
      ],
      "outputs": [],
      "execution_count": 9,
      "metadata": {
        "collapsed": false,
        "outputHidden": false,
        "inputHidden": false,
        "execution": {
          "iopub.status.busy": "2022-06-26T01:23:16.022Z",
          "iopub.execute_input": "2022-06-26T01:23:16.027Z",
          "iopub.status.idle": "2022-06-26T01:23:16.051Z",
          "shell.execute_reply": "2022-06-26T01:23:16.044Z"
        }
      }
    },
    {
      "cell_type": "code",
      "source": [
        "model = LogisticRegression()"
      ],
      "outputs": [],
      "execution_count": 10,
      "metadata": {
        "collapsed": false,
        "outputHidden": false,
        "inputHidden": false,
        "execution": {
          "iopub.status.busy": "2022-06-26T01:23:16.058Z",
          "iopub.execute_input": "2022-06-26T01:23:16.064Z",
          "iopub.status.idle": "2022-06-26T01:23:16.072Z",
          "shell.execute_reply": "2022-06-26T01:23:16.150Z"
        }
      }
    },
    {
      "cell_type": "code",
      "source": [
        "model.fit(X,y)"
      ],
      "outputs": [
        {
          "output_type": "execute_result",
          "execution_count": 11,
          "data": {
            "text/plain": "LogisticRegression()"
          },
          "metadata": {}
        }
      ],
      "execution_count": 11,
      "metadata": {
        "collapsed": false,
        "outputHidden": false,
        "inputHidden": false,
        "execution": {
          "iopub.status.busy": "2022-06-26T01:23:16.079Z",
          "iopub.execute_input": "2022-06-26T01:23:16.085Z",
          "iopub.status.idle": "2022-06-26T01:23:16.095Z",
          "shell.execute_reply": "2022-06-26T01:23:16.154Z"
        }
      }
    },
    {
      "cell_type": "code",
      "source": [
        "model.coef_"
      ],
      "outputs": [
        {
          "output_type": "execute_result",
          "execution_count": 12,
          "data": {
            "text/plain": "array([[-2.76004784e-33, -9.53287302e-33, -1.73770800e-32,\n        -1.73155534e-32, -2.42520568e-32, -1.26930400e-25,\n         9.69451683e-34, -5.11478209e-32,  8.71240799e-32,\n        -2.94365970e-33, -9.46149683e-32, -1.71387947e-31,\n        -1.73438984e-31, -2.44801141e-31, -1.15046226e-24,\n         1.82235314e-32, -2.93325490e-31,  6.14938705e-31,\n        -2.04935715e-32, -2.52982505e-31, -2.50234814e-31,\n        -3.26062696e-31, -1.74624548e-24,  1.68314252e-32,\n        -3.02158807e-31,  6.25609138e-31, -2.11959004e-32,\n        -2.47150308e-31, -3.18780124e-31, -1.71383840e-24,\n         1.68218051e-32, -2.84656140e-31,  6.02873133e-31,\n        -1.99146058e-32, -3.94670486e-31, -2.24080789e-24,\n         1.53138011e-32, -2.92758860e-31,  6.13846982e-31,\n        -2.06039439e-32, -2.82310089e-18, -3.75727860e-26,\n        -2.06305162e-24,  2.90726769e-24, -1.76207005e-25,\n         9.69451683e-34, -4.57717997e-33,  4.80115341e-32,\n        -5.52279392e-34, -2.76004784e-33, -4.00048928e-33,\n        -3.90371664e-34, -2.76004784e-33,  8.01905829e-35,\n        -4.83200679e-35]])"
          },
          "metadata": {}
        }
      ],
      "execution_count": 12,
      "metadata": {
        "collapsed": false,
        "outputHidden": false,
        "inputHidden": false,
        "execution": {
          "iopub.status.busy": "2022-06-26T01:23:16.102Z",
          "iopub.execute_input": "2022-06-26T01:23:16.106Z",
          "iopub.status.idle": "2022-06-26T01:23:16.117Z",
          "shell.execute_reply": "2022-06-26T01:23:16.157Z"
        }
      }
    },
    {
      "cell_type": "code",
      "source": [
        "model.score(X,y)"
      ],
      "outputs": [
        {
          "output_type": "execute_result",
          "execution_count": 13,
          "data": {
            "text/plain": "0.5094017094017094"
          },
          "metadata": {}
        }
      ],
      "execution_count": 13,
      "metadata": {
        "collapsed": false,
        "outputHidden": false,
        "inputHidden": false,
        "execution": {
          "iopub.status.busy": "2022-06-26T01:23:16.126Z",
          "iopub.execute_input": "2022-06-26T01:23:16.130Z",
          "iopub.status.idle": "2022-06-26T01:23:16.139Z",
          "shell.execute_reply": "2022-06-26T01:23:16.161Z"
        }
      }
    }
  ],
  "metadata": {
    "kernel_info": {
      "name": "python3"
    },
    "language_info": {
      "name": "python",
      "version": "3.6.13",
      "mimetype": "text/x-python",
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "pygments_lexer": "ipython3",
      "nbconvert_exporter": "python",
      "file_extension": ".py"
    },
    "kernelspec": {
      "name": "python3",
      "language": "python",
      "display_name": "Python 3"
    },
    "nteract": {
      "version": "0.28.0"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 4
}